/**
 * ReportGear(2011)
 */
package com.reportgear.designer.editor.object;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;

import com.reportgear.designer.gui.component.combobox.JIntComboBox;

/**
 * 整型对象编辑器
 * 
 * @version 1.0 2011-4-26
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class IntegerEditor extends Editor {
	/**
	 * UID
	 */
	private static final long serialVersionUID = -5983995102815571789L;
	private JIntComboBox valueColumnIndexComboBox;

	public IntegerEditor() {
		this(0);
	}

	public IntegerEditor(int size) {
		this(size, null);
	}

	public IntegerEditor(String name) {
		this(0, name);
	}

	public IntegerEditor(int size, String name) {
		setLayout(new BorderLayout());
		this.valueColumnIndexComboBox = new JIntComboBox();
		for (int i = 1; i <= size; ++i)
			this.valueColumnIndexComboBox.addItem(new Integer(i));
		if (size > 0)
			this.valueColumnIndexComboBox.setSelectedInt(1);
		add(this.valueColumnIndexComboBox, BorderLayout.CENTER);

		setName(name);
	}

	public IntegerEditor(String[] StrArr, String name) {
		setLayout(new BorderLayout());
		this.valueColumnIndexComboBox = new JIntComboBox();
		for (int i = 1; i <= StrArr.length; ++i) {
			this.valueColumnIndexComboBox.addItem(new Integer(i));
		}
		this.valueColumnIndexComboBox.setRenderer(new ColumnNameCellRenderer(StrArr));
		if (StrArr.length > 0) {
			this.valueColumnIndexComboBox.setSelectedInt(1);
		}
		add(this.valueColumnIndexComboBox, BorderLayout.CENTER);
		setName(name);
	}

	public Object getValue() {
		return Integer.valueOf(this.valueColumnIndexComboBox.getSelectedInt());
	}

	public void setValue(Object value) {
		this.valueColumnIndexComboBox.setSelectedInt(((Integer) value).intValue());
	}

	public boolean accept(Object value) {
		return (value instanceof Integer);
	}

	@SuppressWarnings("serial")
	private static class ColumnNameCellRenderer extends DefaultListCellRenderer {
		private String[] columnName;

		public ColumnNameCellRenderer(String[] strArray) {
			this.columnName = strArray;
		}

		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
				boolean cellHasFocus) {
			super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
			setText(this.columnName[(((Integer) value).intValue() - 1)]);

			return this;
		}
	}
}